//
// Created by ching on 2023/4/10.
//
#include <random>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct Node {
    int index = 0;
    int distance = 0;
    Node* parent = NULL;
    vector<Node*> children;
};

static int randInt(int min, int max) {
    return min + (rand() % static_cast<int>(max - min + 1));
}

class DTreeUtil {
    Node* nodes;
    Node* root;
    int n;
    int d;
public:
    DTreeUtil(int n, int d) {
        this->n = n;
        this->d = d;
        nodes = new Node[n];
        for(int i = 1; i < n; i++) {
            Node* node = &nodes[i];
            node->index = i;
            node->distance = randInt(1, d * 3);
            Node* parent = &nodes[randInt(0, i - 1)];     //生成任意树
            //Node* parent = &nodes[(i - 1) / 2];         //生成二叉树
            node->parent = parent;
            parent->children.push_back(node);
        }
    }

    void print(ostream &out = cout) {
        out << n << " " << d << endl;
        for(int i = 0; i < n; i++) {
            Node* parent = &nodes[i];
            out << parent->children.size();
            for(vector<Node*>::iterator it = parent->children.begin(); it != parent->children.end(); it++) {
                Node* node = *it;
                out << " " << node->index << " " << node->distance;
            }
            out << endl;
        }
    }
};

int main() {
    srand((unsigned)time(NULL));

    int n = 1000;
    int d = 50;

    DTreeUtil treeUtil(n, d);

    treeUtil.print();

    ostringstream filename;
    filename << n << "_" << d << ".txt";
    ofstream fout(filename.str(), ios::out);
    treeUtil.print(fout);
    fout.close();

    return 0;
}